import { Component, OnInit } from '@angular/core';
import { TransferService } from '../../service/transfer.service';
import { ActivatedRoute, Router } from '@angular/router';
import { UserTransaction } from '../../../../model';
import { switchMap } from 'rxjs/operators';

@Component({
  selector: 'app-transfer-success',
  templateUrl: './transfer-success.component.html',
  styleUrls: ['./transfer-success.component.scss']
})
export class TransferSuccessComponent implements OnInit {

  userTransaction: UserTransaction;
  coinName: string;
  constructor(
    private transferService: TransferService,
    private routes: ActivatedRoute,
    private router: Router,
  ) {
    this.coinName = this.transferService.getCoinName();
  }


  toMemeberCenter() {
    this.router.navigateByUrl('/member/center/home');
  }

  ngOnInit() {
    this.transferService.changeGoBackUrl('/member/transfer');
    this.routes.queryParams.pipe(
      switchMap(res => this.transferService.getTransfer(res.transactionId))
    ).subscribe(res => {
      // tslint:disable-next-line:no-unused-expression
      // this.userTransaction = {
      //   amount: 100,
      //   category: 'daa',
      //   coinName: 'CHP',
      //   completeDtm: new Date().toString(),
      //   confirmation:1,
      //   endDate: new Date().toString(),
      //   ethGas: 1,
      // } as UserTransaction;
      if (res.status === 0) {
        this.userTransaction = res.data;
      } else {
        this.transferService.error(res);
      }
    }, error =>  this.transferService.error(error));
  }

}
